package ps.net;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


public class ClientListContent implements PacketContent {
    
    public static final String DELIMITER = ";";
    
    String[] clients = new String[0];
    
    ClientListContent() {
        this(new String[0]);
    }
    
    public ClientListContent(String[] clients) {
        this.clients = clients;
    }
    
    @Override
    public void writeContent(OutputStream out) throws IOException {
        // write number of clients
        out.write(clients.length);
        
        // write client names
        for (String clientName : clients) {
            Packet.writeString(out, clientName);
        }
    }
    
    @Override
    public void readContent(InputStream in) throws IOException {
        int count = in.read();
        clients = new String[count];
        for (int i = 0; i < clients.length; i++) {
            clients[i] = Packet.readString(in);
        }
    }
    
    @Override
    public String toString() {
        String ret = "[ ClientList |";
        ret += " clients=\"";
        for (int i = 0; i < clients.length; i++) {
            ret += "\"" + clients[i] + "\";";
        }
        ret += "\" ]";
        return ret;
    }
    
    public String[] getClients() {
        return clients;
    }
    
}
